package com.bytedance.retrofit2.client;

import com.bytedance.retrofit2.mime.TypedInput;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public final class SsResponse {
    public final String url;
    public final int status;
    public final List headers;
    public final TypedInput body;
    public Object requestInfo;

    public SsResponse(String url, int statusCode, String reason, List<Header> headers, TypedInput body) {
        if (url == null) {
            throw new IllegalArgumentException("url == null");
        }

        if (statusCode < 200) {
            throw new IllegalArgumentException("Invalid status code: " + statusCode);
        }

        if (reason == null) {
            throw new IllegalArgumentException("reason == null");
        }

        if (headers == null) {
            throw new IllegalArgumentException("headers == null");
        }

        this.url = url;
        this.status = statusCode;
        this.headers = Collections.unmodifiableList(new ArrayList(headers));
        this.body = body;
    }

    public final Header getHeader(String name) {
        if (this.headers == null) {
            return null;
        } else {
            Iterator<Header> iterator = this.headers.iterator();
            while (iterator.hasNext()) {
                Header header = iterator.next();
                if (name.equalsIgnoreCase(header.name)) {
                    return header;
                }
            }
            return null;
        }
    }

    public final boolean success() {
        return this.status == 200;
    }

    public final List<Header> getHeaders(String name) {
        List<Header> headerList = null;
        if (name != null && this.headers != null) {
            Iterator<Header> iterator = this.headers.iterator();
            while (iterator.hasNext()) {
                Header header = iterator.next();
                if (name.equalsIgnoreCase(header.name)) {
                    if (headerList == null) {
                        headerList = new ArrayList();
                    }
                    headerList.add(header);
                }
            }
        }
        return headerList;
    }
}

